home *** CD-ROM | disk | FTP | other *** search
- Path: mayne.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: HELP : Assignment with strings and pointers.
- Date: 9 Apr 1996 11:34:03 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4keairINN3jv@mayne.ugrad.cs.ubc.ca>
- References: <316ac5b5.4283622@news.planet.net>
- NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
-
- In article <316ac5b5.4283622@news.planet.net>,
- David <cygnusx@planet.net> wrote:
- >
- >
- >This program is probably idiotic since I am in a (intro to C ) class
- >but it does work. I was wondering if anyone had a better idea for this
- >type of situation. I could not find an actual function in the
- >character handling library for this? does one exist ? i would like to
- >make a separate function in the main to handle this task? Is it
- >possible?
- >
- >thanx David
- >
- >
- > #include <stdio.h>
- > #include <ctype.h>
- >
- > /* this program determines if the first word is the second word
- >spelled backwords and have only included 3 characters for
- >simplification . inputs have been left out for clarity */
-
-
- You have to use loops to iterate over the characters of two strings
- simultaneously, forward over one and backward over the other. In other words,
- generalize the test you have hand-coded for the special case of three character
- strings.
-
- First you trivially reject the case when the strings have a different length
- (or alternately perform the comparison on the first N characters of both
- strings, where N is the lesser of their two lengths).
-
- #include <stdio.h>
- #include <string.h>
-
- int reversecmp(const char *s1, const char *s2)
-
- {
- size_t L1 = strlen(s1);
- size_t L2 = strlen(s2);
- size_t i, j;
-
- if (L1 != L2)
- return 0;
-
- for (i = 0, j = L2-1; i < L1; i++, j--)
- if (s1[i] != s2[j])
- return 0;
-
- return 1;
- }
- --
-
-